package com.niuwa.p2p.action.channel;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.AppConstants;
import com.niuwa.p2p.core.utils.DictConstant.ChlAuditAction;
import com.niuwa.p2p.core.utils.DictUtil;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.channel.IChannelCustomerService;
import com.niuwa.p2p.vo.channel.AuditFrom;
import com.niuwa.p2p.vo.channel.ChannelAuditLogVo;
import com.niuwa.p2p.vo.channel.ChannelCustomerVo;

public class ChannelCustomerAction extends ChannelCommonAction {

    private static final long serialVersionUID = -3277971463484336743L;

    @Autowired
	private IChannelCustomerService channelCustomerService;
    		
	private AuditFrom auditFrom;

	public AuditFrom getAuditFrom() {
		return auditFrom;
	}

	public void setAuditFrom(AuditFrom auditFrom) {
		this.auditFrom = auditFrom;
	}		


	/**
	 * 跳转到渠道客户列表
	 * 
	 * @return 渠道客户列表试图
	 * 
	 */	
	@Action("gotoChannelCustomerList")
	public String gotoChannelCustomerList() {
		return "channelCustomerList";
	}
	
	/**
	 *  渠道客户列表分页查询
	 * @return
	 */
 	@Action("listChannelCustomer")
 	public String listChannelCustomer() { 	
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		// 执行查询操作
		PageBean<ChannelCustomerVo>  pagination =  channelCustomerService.search(paramMap);
		// 设置返回		
		setObjectReSult(pagination);
		return NONE;
 	}
 	
	/**
	 * 跳转到审批操作记录
	 * 
	 * @return 审批操作记录视图
	 * 
	 */	
	@Action("gotoAuditOperateRecordList")
	public String gotoAuditOperateRecordList() {
		return "auditOperateRecordList";
	}
 	
	/**
	 * 审批操作记录列表分页查询
	 * @return
	 */
	@Action("listAuditOperateRecord")
	public String listAuditOperateRecord() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		paramMap.put("corpCode", paramMap.get("channelCode"));
		// 执行查询操作
		PageBean<ChannelAuditLogVo>  pagination =  channelCustomerService.listAuditOperateRecord(paramMap);
		// 设置返回		
		setObjectReSult(pagination);
		return NONE;
	}
 	
	/**
	 * 提交审核
	 * @throws Exception 
	 */	
	@Action("toAuditChannel")
	public String toAuditChannel() throws Exception {
		JsonResult result = new JsonResult();		
		channelCustomerService.updateToAuditChannel(this.getChannelCode(), SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
			
	/**
	 * 审核
	 * @throws Exception 
	 */	
	@Action("auditChannel")
	public String auditChannel() throws Exception {
		JsonResult result = new JsonResult();
		String errorMsg = toAuditChannelFiledCheck();
		if(!StringUtil.isEmpty(errorMsg)) {
			result.setSuccess(false);
			result.setMsg(errorMsg);
			this.setResult(result);
			return NONE;
		}
		channelCustomerService.updateAuditChannel(this.getChannelCode(), auditFrom, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;	
	}
	
	/**
	 * 启用
	 * @throws Exception 
	 */	
	@Action("enableChannel")
	public String enableChannel() throws Exception {
		JsonResult result = new JsonResult();		
		channelCustomerService.updateEnableChannel(this.getChannelCode(), SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 添加渠道
	 */	
    @Action("creatChannel")
    public String createChannel() {
    	this.setChannelOperateFlg(AppConstants.OPER_ADD);
        return "channelTab";
    }
	
	/**
	 * 察看渠道
	 */	
	@Action("viewChannel")
	public String viewChannel() {
		this.setChannelOperateFlg(AppConstants.OPER_VIEW);
		return "channelTab";
	}
	
	/**
	 * 编辑渠道
	 */	
	@Action("editChannel")
	public String editChannel() {
		this.setChannelOperateFlg(AppConstants.OPER_EDIT);
		return "channelTab";
	}	
    
	/**
	 * 加载渠道基本信息
	 */	
    @Action("loadChannelBaseInfo")
    public String loadChannelBaseInfo() {
    	loadChannelBasicInfoVo();
        return "channelBaseInfo";
    }
    
	/**
	 * 加载渠道基本信息
	 */	
    @Action("loadChannelInfoCheck")
    public String loadChannelInfoCheck() {
    	loadChannelBasicInfoVo();
        return "channelInfoCheck";
    }
    
    /**
	 * 加载渠道影像文件上传
	 */	
    @Action("loadChannelImageFileUpload")
    public String loadChannelImageFileUpload() {
    	loadChannelBasicInfoVo();
        return "channelImageFileUpload";
    }
    
	/**
	 *  加载渠道外访文件上传
	 */	
    @Action("loadChannelVisitFileUpload")
    public String loadChannelVisitFileUpload() {
    	loadChannelBasicInfoVo();
        return "channelVisitFileUpload";
    }    
		
	/**
	 *  加载渠道信息
	 */	
	private void loadChannelBasicInfoVo(){
    	if(!StringUtil.isEmpty(this.getChannelCode())){
    		this.setCbiv(channelCustomerService.getChannelBasicInfo(this.getChannelCode())); 
    	}   
	}
	
	/**
	 * 提交审核信息check
	 * 
	 * @return check信息(无错误:"")
	 */
	private String toAuditChannelFiledCheck(){
		
		final String CHL_AUDIT_ACTION = "CHL_AUDIT_ACTION";
		String operateType = auditFrom.getOperateType();
		if(StringUtil.isEmpty(operateType)) {
			return this.getText("channelCustomerAction.operateType.RequiredString.message");
		}
		if(StringUtil.isEmpty(DictUtil.getDictNameByTypeAndCode(CHL_AUDIT_ACTION, operateType))) {
			return this.getText("channelCustomerAction.operateType.TypeError.message");
		}
		
		// 设置渠道审核类型
		if(ChlAuditAction.PASS.getCode().equals(auditFrom.getOperateType())) {
			if(null == auditFrom.getApproveAmount()) {
				return this.getText("channelCustomerAction.approveAmount.RequiredString.message");
			}	
		}else if(ChlAuditAction.RETURN.getCode().equals(auditFrom.getOperateType())
				|| ChlAuditAction.REJECT.getCode().equals(auditFrom.getOperateType())) {
			if(StringUtil.isEmpty(auditFrom.getRemark())) {
				return this.getText("channelCustomerAction.remark.RequiredString.message");
			}
			if(auditFrom.getRemark().length() > 100) {
				return this.getText("channelCustomerAction.remark.StringLength.message");
			}	
		}
				
		return "";
	}
		
}
